package io.gitee.chearnee.fw.socket.component.codec;

import io.gitee.chearnee.fw.socket.base.SocketConstant;
import io.gitee.chearnee.fw.socket.entity.MsgEntity;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.List;

public class NettyMessageEncode  extends MessageToMessageEncoder<MsgEntity> {
    @Override
    protected void encode(ChannelHandlerContext ctx, MsgEntity msg, List<Object> out) throws Exception {
        String s = SocketConstant.getObjectMapper().writeValueAsString(msg);
        out.add(ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(s), Charset.defaultCharset()));
    }
}
